import java.util.Arrays;
import java.util.PriorityQueue;

public class Solution973 {
    public int[][] kClosest(int[][] points, int k) {
        PriorityQueue<int[]> queue=new PriorityQueue<>((a,b)->b[2]-a[2]);
        for (int[] point : points) {
            queue.offer(new int[]{point[0],point[1],point[0]*point[0]+point[1]*point[1]});
            if (queue.size()>k){
                queue.poll();
            }
        }
        int[][] ans=new int[k][2];
        for (int i=0;i<k;i++){
            ans[i]= Arrays.copyOf(queue.poll(),2);
        }
        return ans;
    }

    public static void main(String[] args) {
        int[][] ans=new Solution973().kClosest(new int[][]{{1,3},{-2,2}},1);
        for (int[] an : ans) {
            System.out.println(Arrays.toString(an));
        }
    }
}
